<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: IT宇宙人
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Admin\Logic\GoodsLogic;
use Think\AjaxPage;
use Think\Page;

class GoodsController extends BaseController {

	/**
	 *  商品列表
	 */
	public function goodsList() {

		$this -> display();
	}

	/**
	 *  路线列表
	 */
	public function ajaxGoodsList() {

		$model = M('product');
		$where = "1=1";
		$count = $model -> where($where) -> count();
		$Page = new AjaxPage($count, 10);

		$show = $Page -> show();
		$desc_list = M('desc') -> getField('value,desc');
		$topic_list = C('TOPIC_LIST');
		$goodsList = $model -> where($where) -> order('id desc') -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
		foreach ($goodsList as $key => $value) {
			$baseinfo = json_decode($value['p_baseinfo'], true);
			$goodsList[$key]['name'] = $baseinfo['name'];
			$goodsList[$key]['desc'] = $desc_list[$value['destination']];
			$goodsList[$key]['topic'] = $topic_list[$value['topic']];
		}

		$this -> assign('goodsList', $goodsList);
		$this -> assign('page', $show);
		// 赋值分页输出
		$this -> display();
	}

	/**
	 * 添加修改商品
	 */
	public function addEditGoods() {

		$id = $_GET['id'];
		if ($id) {
			$info = M('product') -> where("id = $id") -> find();
			$baseinfo = json_decode($info['p_baseinfo'], true);
			$trafficinfo = json_decode($info['p_trafficinfo'], true);
			$scheduleinfo = json_decode($info['p_schedule'], true);

		}
		$desc = M('desc') -> select();
		// 商品相册
		$this -> initEditor();
		// 编辑器

		$this -> assign('baseinfo', $baseinfo);
		$this -> assign('trafficinfo', $trafficinfo);
		$this -> assign('scheduleinfo', $scheduleinfo);
		$this -> assign('topic', $info['topic']);
		$this -> assign('hotcity', $info['hotcity']);
		$this -> assign('daynum', count($scheduleinfo) + 1);
		$this -> assign('destination', $info['destination']);

		$this -> assign('desc', $desc);
		$this -> display('_goods');
	}

	public function addEditGoodsAjax() {

		//去除最后一个空元素

		$c = count($_POST['baseinfo']['pictures']) - 1;
		unset($_POST['baseinfo']['pictures'][$c]);

		$_POST['baseinfo']['logo'] = $_POST['baseinfo']['logo'][0];

		$b = count($_POST['trafficinfo']['hotel']['images']) - 1;
		unset($_POST['trafficinfo']['hotel']['images'][$b]);

		$data['p_baseinfo'] = json_encode($_POST['baseinfo']);

		$data['p_trafficinfo'] = json_encode($_POST['trafficinfo']);

		//序列化schedule
		$scheduleinfo = $_POST['scheduleinfo'];

		foreach ($scheduleinfo['route'] as $key => $value) {

			//$data['p_schedule'][$key]['num'] = $data['num'][$key];
			$data['p_schedule'][$key]['route'] = $scheduleinfo['route'][$key];
			$data['p_schedule'][$key]['hotel'] = $scheduleinfo['hotel'][$key];
			$data['p_schedule'][$key]['food'] = $scheduleinfo['food'][$key];
			$data['p_schedule'][$key]['schedule'] = $scheduleinfo['schedule'][$key];
			$data['p_schedule'][$key]['content'] = $scheduleinfo['content'][$key];
		}
		//去除最后一个空元素
		$count = count($data['p_schedule']) - 1;
		unset($data['p_schedule'][$count]);

		$data['p_schedule'] = json_encode($data['p_schedule']);
		$data['status'] = 1;
		$data['destination'] = $_POST['baseinfo']['destination'];
		$data['topic'] = $_POST['baseinfo']['topic'];
		$data['hotcity'] = $_POST['baseinfo']['hotcity'] == 'on' ? 1 : 0;
		$data['ishot'] = $_POST['baseinfo']['ishot'] == 'on' ? 1 : 0;
		$data['price'] = $_POST['baseinfo']['price'];
		$data['name'] = $_POST['baseinfo']['name'];
		if ($_POST['id']) {

			$ret = M('product') -> where("id = {$_POST['id']}") -> save($data);
			if ($ret !== FALSE) {
				$back['status'] = 1;
				$back['msg'] = "保存成功";

			} else {
				$back['status'] = -1;
				$back['msg'] = "保存失败";

			}

		} else {
			$ret = M('product') -> add($data);
			if ($ret) {

				$back['status'] = 1;
				$back['msg'] = "添加成功";

			} else {

				$back['status'] = -1;
				$back['msg'] = "添加失败";

			}
		}

		$this -> ajaxReturn($back);

	}

	//目的地管理
	public function descList() {
		$list = M('desc') -> select();

		$this -> assign('list', $list);
		$this -> display();

	}

	//添加目的地
	public function add_desc() {

		if (IS_POST) {
			$data = $_POST;
			M('desc') -> add($data);

			header("Location:" . U('descList'));
			exit ;
		}
		$this -> display();
	}

	public function del_desc($id) {
		M('desc') -> where(array('id' => $id)) -> delete();
		header("Location:" . U('descList'));

	}

	/**
	 * 商品类型  用于设置商品的属性
	 */
	public function goodsTypeList() {
		$model = M("GoodsType");
		$count = $model -> count();
		$Page = new Page($count, 100);
		$show = $Page -> show();
		$goodsTypeList = $model -> order("id desc") -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
		$this -> assign('show', $show);
		$this -> assign('goodsTypeList', $goodsTypeList);
		$this -> display('goodsTypeList');
	}

	/**
	 * 添加修改编辑  商品属性类型
	 */
	public function addEditGoodsType() {
		$_GET['id'] = $_GET['id'] ? $_GET['id'] : 0;
		$model = M("GoodsType");
		if (IS_POST) {
			$model -> create();
			if ($_GET['id'])
				$model -> save();
			else
				$model -> add();

			$this -> success("操作成功!!!", U('Admin/Goods/goodsTypeList'));
			exit ;
		}
		$goodsType = $model -> find($_GET['id']);
		$this -> assign('goodsType', $goodsType);
		$this -> display('_goodsType');
	}

	/**
	 * 添加修改编辑  商品属性
	 */
	public function addEditGoodsAttribute() {

		$model = D("GoodsAttribute");
		$type = $_POST['attr_id'] > 0 ? 2 : 1;
		// 标识自动验证时的 场景 1 表示插入 2 表示更新
		$_POST['attr_values'] = str_replace('_', '', $_POST['attr_values']);
		// 替换特殊字符
		$_POST['attr_values'] = str_replace('@', '', $_POST['attr_values']);
		// 替换特殊字符
		$_POST['attr_values'] = trim($_POST['attr_values']);

		if (($_GET['is_ajax'] == 1) && IS_POST)//ajax提交验证
		{
			C('TOKEN_ON', false);
			if (!$model -> create(NULL, $type))// 根据表单提交的POST数据创建数据对象
			{
				//  编辑
				$return_arr = array('status' => -1, 'msg' => '', 'data' => $model -> getError(), );
				$this -> ajaxReturn(json_encode($return_arr));
			} else {
				// C('TOKEN_ON',true); //  form表单提交
				if ($type == 2) {
					$model -> save();
					// 写入数据到数据库
				} else {
					$insert_id = $model -> add();
					// 写入数据到数据库
				}
				$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => array('url' => U('Admin/Goods/goodsAttributeList')), );
				$this -> ajaxReturn(json_encode($return_arr));
			}
		}
		// 点击过来编辑时
		$_GET['attr_id'] = $_GET['attr_id'] ? $_GET['attr_id'] : 0;
		$goodsTypeList = M("GoodsType") -> select();
		$goodsAttribute = $model -> find($_GET['attr_id']);
		$this -> assign('goodsTypeList', $goodsTypeList);
		$this -> assign('goodsAttribute', $goodsAttribute);
		$this -> display('_goodsAttribute');
	}

	/**
	 * 更改指定表的指定字段
	 */
	public function updateField() {
		$primary = array('goods' => 'goods_id', 'goods_category' => 'id', 'brand' => 'id', 'goods_attribute' => 'attr_id', 'ad' => 'ad_id', );
		$model = D($_POST['table']);
		$model -> $primary[$_POST['table']] = $_POST['id'];
		$model -> $_POST['field'] = $_POST['value'];
		$model -> save();
		$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => array('url' => U('Admin/Goods/goodsAttributeList')), );
		$this -> ajaxReturn(json_encode($return_arr));
	}

	/**
	 * 动态获取商品属性输入框 根据不同的数据返回不同的输入框类型
	 */
	public function ajaxGetAttrInput() {
		$GoodsLogic = new GoodsLogic();
		$str = $GoodsLogic -> getAttrInput($_REQUEST['goods_id'], $_REQUEST['type_id']);
		exit($str);
	}

	/**
	 * 删除商品
	 */
	public function delGoods() {
		$goods_id = $_GET['id'];

		$ret = M('product') -> where("id ={$goods_id}") -> delete();
		if ($ret !== FALSE) {

			$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '', );

		} else {

			$return_arr = array('status' => -1, 'msg' => '操作失败', 'data' => '', );
		}

		//$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
		$this -> ajaxReturn(json_encode($return_arr));
	}

	/**
	 * 删除商品类型
	 */
	public function delGoodsType() {
		// 判断 商品规格
		$count = M("Spec") -> where("type_id = {$_GET['id']}") -> count("1");
		$count > 0 && $this -> error('该类型下有商品规格不得删除!', U('Admin/Goods/goodsTypeList'));
		// 判断 商品属性
		$count = M("GoodsAttribute") -> where("type_id = {$_GET['id']}") -> count("1");
		$count > 0 && $this -> error('该类型下有商品属性不得删除!', U('Admin/Goods/goodsTypeList'));
		// 删除分类
		M('GoodsType') -> where("id = {$_GET['id']}") -> delete();
		$this -> success("操作成功!!!", U('Admin/Goods/goodsTypeList'));
	}

	/**
	 * 删除商品属性
	 */
	public function delGoodsAttribute() {
		// 判断 有无商品使用该属性
		$count = M("GoodsAttr") -> where("attr_id = {$_GET['id']}") -> count("1");
		$count > 0 && $this -> error('有商品使用该属性,不得删除!', U('Admin/Goods/goodsAttributeList'));
		// 删除 属性
		M('GoodsAttribute') -> where("attr_id = {$_GET['id']}") -> delete();
		$this -> success("操作成功!!!", U('Admin/Goods/goodsAttributeList'));
	}

	/**
	 * 删除商品规格
	 */
	public function delGoodsSpec() {
		// 判断 商品规格项
		$count = M("SpecItem") -> where("spec_id = {$_GET['id']}") -> count("1");
		$count > 0 && $this -> error('清空规格项后才可以删除!', U('Admin/Goods/specList'));
		// 删除分类
		M('Spec') -> where("id = {$_GET['id']}") -> delete();
		$this -> success("操作成功!!!", U('Admin/Goods/specList'));
	}

	/**
	 * 品牌列表
	 */
	public function brandList() {
		$model = M("Brand");
		$where = "";
		$keyword = I('keyword');
		$where = $keyword ? " name like '%$keyword%' " : "";
		$count = $model -> where($where) -> count();
		$Page = new Page($count, 10);
		$brandList = $model -> where($where) -> order("`sort` asc") -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
		$show = $Page -> show();
		$cat_list = M('goods_category') -> where("parent_id = 0") -> getField('id,name');
		// 已经改成联动菜单
		$this -> assign('cat_list', $cat_list);
		$this -> assign('show', $show);
		$this -> assign('brandList', $brandList);
		$this -> display('brandList');
	}

	/**
	 * 添加修改编辑  商品品牌
	 */
	public function addEditBrand() {
		$id = I('id');
		$model = M("Brand");
		if (IS_POST) {
			$model -> create();
			if ($id)
				$model -> save();
			else
				$id = $model -> add();

			$this -> success("操作成功!!!", U('Admin/Goods/brandList', array('p' => $_GET['p'])));
			exit ;
		}
		$cat_list = M('goods_category') -> where("parent_id = 0") -> select();
		// 已经改成联动菜单
		$this -> assign('cat_list', $cat_list);
		$brand = $model -> find($id);
		$this -> assign('brand', $brand);
		$this -> display('_brand');
	}

	/**
	 * 删除品牌
	 */
	public function delBrand() {
		// 判断此品牌是否有商品在使用
		$goods_count = M('Goods') -> where("brand_id = {$_GET['id']}") -> count('1');
		if ($goods_count) {
			$return_arr = array('status' => -1, 'msg' => '此品牌有商品在用不得删除!', 'data' => '', );
			//$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
			$this -> ajaxReturn(json_encode($return_arr));
		}

		$model = M("Brand");
		$model -> where('id =' . $_GET['id']) -> delete();
		$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '', );
		//$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
		$this -> ajaxReturn(json_encode($return_arr));
	}

	/**
	 * 初始化编辑器链接
	 * 本编辑器参考 地址 http://fex.baidu.com/ueditor/
	 */
	private function initEditor() {
		$this -> assign("URL_upload", U('Admin/Ueditor/imageUp', array('savepath' => 'goods')));
		// 图片上传目录
		$this -> assign("URL_imageUp", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
		//  不知道啥图片
		$this -> assign("URL_fileUp", U('Admin/Ueditor/fileUp', array('savepath' => 'article')));
		// 文件上传s
		$this -> assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp', array('savepath' => 'article')));
		//  图片流
		$this -> assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage', array('savepath' => 'article')));
		// 远程图片管理
		$this -> assign("URL_imageManager", U('Admin/Ueditor/imageManager', array('savepath' => 'article')));
		// 图片管理
		$this -> assign("URL_getMovie", U('Admin/Ueditor/getMovie', array('savepath' => 'article')));
		// 视频上传
		$this -> assign("URL_Home", "");
	}

	/**
	 * 商品规格列表
	 */
	public function specList() {
		$goodsTypeList = M("GoodsType") -> select();
		$this -> assign('goodsTypeList', $goodsTypeList);
		$this -> display();
	}

	/**
	 *  商品规格列表
	 */
	public function ajaxSpecList() {
		//ob_start('ob_gzhandler'); // 页面压缩输出
		$where = ' 1 = 1 ';
		// 搜索条件
		I('type_id') && $where = "$where and type_id = " . I('type_id');
		// 关键词搜索
		$model = D('spec');
		$count = $model -> where($where) -> count();
		$Page = new AjaxPage($count, 13);
		$show = $Page -> show();
		$specList = $model -> where($where) -> order('`type_id` desc') -> limit($Page -> firstRow . ',' . $Page -> listRows) -> select();
		$GoodsLogic = new GoodsLogic();
		foreach ($specList as $k => $v) {// 获取规格项
			$arr = $GoodsLogic -> getSpecItem($v['id']);
			$specList[$k]['spec_item'] = implode(' , ', $arr);
		}

		$this -> assign('specList', $specList);
		$this -> assign('page', $show);
		// 赋值分页输出
		$goodsTypeList = M("GoodsType") -> select();
		// 规格分类
		$goodsTypeList = convert_arr_key($goodsTypeList, 'id');
		$this -> assign('goodsTypeList', $goodsTypeList);
		$this -> display();
	}

	/**
	 * 添加修改编辑  商品规格
	 */
	public function addEditSpec() {

		$model = D("spec");
		$type = $_POST['id'] > 0 ? 2 : 1;
		// 标识自动验证时的 场景 1 表示插入 2 表示更新
		if (($_GET['is_ajax'] == 1) && IS_POST)//ajax提交验证
		{
			C('TOKEN_ON', false);
			if (!$model -> create(NULL, $type))// 根据表单提交的POST数据创建数据对象
			{
				//  编辑
				$return_arr = array('status' => -1, 'msg' => '', 'data' => $model -> getError(), );
				$this -> ajaxReturn(json_encode($return_arr));
			} else {
				// C('TOKEN_ON',true); //  form表单提交
				if ($type == 2) {
					$model -> save();
					// 写入数据到数据库
					$model -> afterSave($_POST['id']);
				} else {
					$insert_id = $model -> add();
					// 写入数据到数据库
					$model -> afterSave($insert_id);
				}
				$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => array('url' => U('Admin/Goods/specList')), );
				$this -> ajaxReturn(json_encode($return_arr));
			}
		}
		// 点击过来编辑时
		$id = $_GET['id'] ? $_GET['id'] : 0;
		$spec = $model -> find($id);
		$GoodsLogic = new GoodsLogic();
		$items = $GoodsLogic -> getSpecItem($id);
		$spec[items] = implode(PHP_EOL, $items);
		$this -> assign('spec', $spec);

		$goodsTypeList = M("GoodsType") -> select();
		$this -> assign('goodsTypeList', $goodsTypeList);
		$this -> display('_spec');
	}

	/**
	 * 动态获取商品规格选择框 根据不同的数据返回不同的选择框
	 */
	public function ajaxGetSpecSelect() {
		$goods_id = $_GET['goods_id'] ? $_GET['goods_id'] : 0;
		$GoodsLogic = new GoodsLogic();
		//$_GET['spec_type'] =  13;
		$specList = D('Spec') -> where("type_id = " . $_GET['spec_type']) -> order('`order` desc') -> select();
		foreach ($specList as $k => $v)
			$specList[$k]['spec_item'] = D('SpecItem') -> where("spec_id = " . $v['id']) -> getField('id,item');
		// 获取规格项

		$items_id = M('SpecGoodsPrice') -> where('goods_id = ' . $goods_id) -> getField("GROUP_CONCAT(`key` SEPARATOR '_') AS items_id");
		$items_ids = explode('_', $items_id);

		// 获取商品规格图片
		if ($goods_id) {
			$specImageList = M('SpecImage') -> where("goods_id = $goods_id") -> getField('spec_image_id,src');
		}
		$this -> assign('specImageList', $specImageList);

		$this -> assign('items_ids', $items_ids);
		$this -> assign('specList', $specList);
		$this -> display('ajax_spec_select');
	}

	/**
	 * 动态获取商品规格输入框 根据不同的数据返回不同的输入框
	 */
	public function ajaxGetSpecInput() {
		$GoodsLogic = new GoodsLogic();
		$goods_id = $_REQUEST['goods_id'] ? $_REQUEST['goods_id'] : 0;
		$str = $GoodsLogic -> getSpecInput($goods_id, $_POST['spec_arr']);
		exit($str);
	}

}
